//
//  MListServices.m
//  NeusoftHBSJSober
//
//  Created by Apple-21 on 15/10/28.
//  Copyright (c) 2015年 com.neusoftsober. All rights reserved.
//

#import "MListServices.h"
#import "MListModel.h"
#define KID_WORD_URL  [NSString stringWithFormat:@"%@%@",BASE_SERVER_URL,@"/api/HotKeyword"]

// 获取kid
#define PRODUCT_KID_URL  [NSString stringWithFormat:@"%@%@",BASE_SERVER_URL,@"/keyword/getkeyword"]

// getpath  参数kid
#define KEYWODE_ATTRIBUTE_URL  [NSString stringWithFormat:@"%@%@",BASE_SERVER_URL,@"/keyword/GetKeywordAttribute"]

#define PRODUCT_LIST_URL  [NSString stringWithFormat:@"%@%@",BASE_SERVER_URL,@"/product/searchproduct"]

//#define MERCHANT_PRODUCT_URL  [NSString stringWithFormat:@"%@%@",BASE_SERVER_URL,@"/product/getproductofsaleman"]
#define MERCHANT_PRODUCT_URL [NSString stringWithFormat:@"%@%@",BASE_SERVER_URL,@"/product/getproductofsaleman"]
@implementation MListServices
// 父类方法重写，单例
+ (id)sharedClient {
    
    static dispatch_once_t pred;
    // 不同的Service不同的单例
    static MListServices *shared_instance = nil;
    
    // 创建单例时使用
    dispatch_once(&pred,^{
        
        shared_instance = [[self alloc]init];
        
        shared_instance.client = [self sharedHTTPClient];// 直接使用父类的方法。
    });
    
    return shared_instance;
}
- (void)getHotKeywordComletion:(getHotKeyword)success failure:(getProductErrorHandler)failure {
    
    
    NSString *path = KID_WORD_URL;
    
    [self.client getPath:path
              parameters:nil
                 success:^(AFHTTPRequestOperation *operation, id responseObject){
                     
                     id JSON = ((AFJSONRequestOperation *)operation).responseJSON;
                     if ([JSON isKindOfClass:[NSArray class]]) {
                         
                         NSMutableArray *array = [NSMutableArray arrayWithArray:JSON];
                         
                         
//                         NSLog(@"array is  ****** %@",arr);
                         success(array);
                     }
                 } failure:^(AFHTTPRequestOperation *operation,NSError *error){
                     
                     
                     failure(@"网络异常");
                 }];
}
#pragma mark 获得kid
- (NSString *)getKIDwithKeyword:(NSString *)keyword completion:(getKIDComletionHandler)success failure:(getProductErrorHandler)failure {
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    if (keyword) {
        [params setObject:keyword forKey:@"name"];
        
    }
    NSString *path = PRODUCT_KID_URL;
    [self.client postPath:path
               parameters:params
                  success:^(AFHTTPRequestOperation *operation, id responseObject){
                      
                      id JSON = ((AFJSONRequestOperation *)operation).responseJSON;
                      if (!JSON) {
                          failure(@"错误");
                          return ;
                      }
                      NSString *data = [((NSNumber *)JSON[@"KID"]) stringValue];
                      success(data);
                  } failure:^(AFHTTPRequestOperation *operation,NSError *error){
                      
                      failure(@"网络异常");
                  }];
    return nil;
}
#pragma mark 通过kid获得ktype
- (void)getKTypeWithKid:(NSString *)kid comletion:(getKTypeComletionHandler)success failure:(getProductErrorHandler)failure {
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    if (kid) {
        [params setObject:kid forKey:@"kid"];
    }
    NSString *path = KEYWODE_ATTRIBUTE_URL;
    [self.client getPath:path
              parameters:params
                 success:^(AFHTTPRequestOperation *operation, id responseObject) {
                     
                     id JSON = ((AFJSONRequestOperation *)operation).responseJSON;
                     if ([JSON isKindOfClass:[NSDictionary class]]) {
                         NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:JSON];
                         success(dict);
                     }
                 }
                 failure:^(AFHTTPRequestOperation *operation,NSError *error){
                     
                     failure(@"网络异常");
                 }];
}
- (void)getProductListWithKid:(NSString *)kid latitude:(NSString *)latitude longtitude:(NSString *)longtitude radius:(NSString *)radius sort:(NSString *)sort typeId:(NSString *)typeID priceId:(NSString *)priceId sizeId:(NSString *)sizeId pageIndex:(NSString *)pageIndex completion:(getProductListCompletionHander)success failure:(getProductErrorHandler)failure {

    NSMutableDictionary *pramas = [[NSMutableDictionary alloc]init];
    if (kid) {
        [pramas setObject:kid forKey:@"kid"];
    }
    if (latitude) {
        [pramas setObject:latitude forKey:@"lat"];
    }
    if (longtitude) {
        [pramas setObject:longtitude forKey:@"lng"];
    }
    if (radius) {
        [pramas setObject:radius forKey:@"radius"];
    }
    if (sort) {
        [pramas setObject:sort forKey:@"sort"];
    }
    if (typeID) {
        [pramas setObject:typeID forKey:@"typeid"];
    }
    if (priceId) {
        [pramas setObject:priceId forKey:@"priceid"];
    }
    if (sizeId) {
        [pramas setObject:sizeId forKey:@"sizeid"];
    }
    if (pageIndex) {
        [pramas setObject:pageIndex forKey:@"pageIndex"];
    }else {
        [pramas setObject:@"0" forKey:@"pageIndex"];
    }
    NSString *path = PRODUCT_LIST_URL;
    if (pramas.count > 0) {
        path = [NSString stringWithFormat:@"%@?",path];
        for (id key in pramas) {
            path = [NSString stringWithFormat:@"%@%@=%@&",path,key,pramas[key]];
        }
    }
    [self.client postPath:path
               parameters:pramas
                  success:^(AFHTTPRequestOperation *operation, id responseObject){
                  
                      id JSON = ((AFJSONRequestOperation *)operation).responseJSON;
                      if (!JSON) {
                          failure(@"data request failure");
                          return ;
                      }
                      
                      NSArray *data = JSON[@"contents"];
                      
                      success([[[MListHttpResult alloc]init] getListResultWithArrayJSON:data]);
                      
                  }
                  failure:^(AFHTTPRequestOperation *operation,NSError *error) {
                            
                            failure(@"网络出错");
                  }];
}
- (void)getMerchantProductListWithMerchantID:(NSString *)MerchantID comletion:(getProductListCompletionHander)success failure:(getProductErrorHandler)failure {

    
    NSDictionary *param = [[NSDictionary alloc]initWithObjects:@[MerchantID,@"99",@"1000",@"Price"] forKeys:@[@"userID",@"Top",@"pageSize",@"sort"]];
    
    [self.client getPath:MERCHANT_PRODUCT_URL parameters:param success:^(AFHTTPRequestOperation *operation ,id responseObject){
        
        NSArray *data = ((AFJSONRequestOperation *)operation).responseJSON;
        if (!data) {
            failure(@"merchant错误");
            return ;
        }
        success([[[MListHttpResult alloc]init]getListResultWithArrayJSON:data]);
        
        
    }failure:^(AFHTTPRequestOperation *operation,NSError *error){
        failure(@"merchant网络异常");
    }];
}
@end
